package com.smart.web.controller;

import com.smart.domain.User;
import com.smart.exception.UserExistException;
import com.smart.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: RegisterConroller
 * @Auther: Liang
 * @Description:
 * @Date: 2018/6/25 21:40
 * @Version: 1.0
 */
@Controller
public class RegisterConroller extends BaseController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/regiset", method = RequestMethod.POST)
    public ModelAndView register(HttpServletRequest request, User user) {
        ModelAndView view = new ModelAndView();
        view.setViewName("/success");
        try {
            userService.register(user);
        } catch (UserExistException e) {
            view.addObject("errorMsg", "用户名已经存在，请选择其他的名字");
            view.setViewName("forward:/register.jsp");
        }
        setSessionUser(request, user);
        return view;
    }
}
